﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace dsjsports
{
    public class Synchronizer
    {
        public Synchronizer()
        {
            DataSetOpenFileDialog.Filter = "Access2010|*.accdb|Access2003|*.mdb|SQL|*.sql";
            SynchronizationThread.Start(queueSynchronization, ref adresDataaBase, ref status);
        }

        public OpenFileDialog DataSetOpenFileDialog = new OpenFileDialog();

        private string status;
        public string Status
        {
            get { return status; }
        }

        private string adresDataaBase;
        public string AdresDataaBase
        {
            get { return adresDataaBase; }
            set { adresDataaBase = value; }
        }

        //подгрузка из бд
        //дописать подгрузку дерева
        public bool DataSet(ref SportsmenList sportsmens)
        {
            try
            {
                sportsmens.Clear();

                string setString = "SELECT спортсмен, имя, фамилия, отчество, пол, дата_рождения, разряд, город, клуб FROM спортсмен";
                OleDbConnection DataConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + adresDataaBase);
                DataConnection.Open();

                OleDbCommand SQLQuery = new OleDbCommand();
                SQLQuery.CommandText = setString;
                SQLQuery.Connection = DataConnection;

                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(SQLQuery);
                DataTable data = new DataTable();
                dataAdapter.Fill(data);

                DataConnection.Close();

                foreach (DataRow r in data.Rows)
                {
                    object[] arr = r.ItemArray;
                    sportsmens.Add(new Sportsmen((int)arr[0], (string)arr[1], (string)arr[3], (string)arr[2], SportsRankCinvert((string)arr[6]), (DateTime)arr[5], FloorCinvert((string)arr[4]), (string)arr[7], (string)arr[8]));
                }
                return true;
            }
            catch
            {
                status = "база данных недоступна";
                return false;
            }
        }

        public void AdditionSportsmen(Sportsmen newSportsmen)
        {
            SynchronizationAddSportsmen sa = new SynchronizationAddSportsmen(newSportsmen, "спортсмен", adresDataaBase, newSportsmen.ID, newSportsmen.Name, newSportsmen.MiddleName, newSportsmen.LastName,
                                                            newSportsmen.SportsRank, newSportsmen.DateOfBirth, newSportsmen.Floor, newSportsmen.Sity, newSportsmen.Clab);
            queueSynchronization.Enqueue(sa);
        }
        public void RemovalSportsmen(Sportsmen sportsmen)
        {
            SynchronizationRemoval sr = new SynchronizationRemoval(sportsmen, "спортсмен", adresDataaBase, sportsmen.ID);
            queueSynchronization.Enqueue(sr);
        }
        //string updateString = "UPDATE " + TableName + " SET " + PropertyName + " = '" + NewValue + "' WHERE " + KeyName + " = " + Id;
        public void ChangeSportsmen(Sportsmen sportsme, string PropertyName, string NewValue, string Id)
        {
            SynchronizationChange sc = new SynchronizationChange(sportsme, adresDataaBase, PropertyName, NewValue, Id);
            queueSynchronization.Enqueue(sc);
        }

        private Queue<SynchronizationInfo> queueSynchronization = new Queue<SynchronizationInfo>();
        public Queue<SynchronizationInfo> QueueSynchronization
        {
            get { return queueSynchronization; }
        }

        //чтоб с бд было проше общаться
        private SportsRank SportsRankCinvert(string sr)
        {
            switch (sr)
            {
                case "МСМК":
                    return SportsRank.МСМК;
                case "МС":
                    return SportsRank.МС;
                case "КМС":
                    return SportsRank.КМС;
                case "I":
                    return SportsRank.I;
                case "II":
                    return SportsRank.II;
                case "III":
                    return SportsRank.III;
                case "Iю":
                    return SportsRank.Iю;
                case "IIю":
                    return SportsRank.IIю;
                case "IIIю":
                    return SportsRank.IIIю;
                case "бр":
                    return SportsRank.бр;
                default:
                    return SportsRank.бр;
            }
        }
        private Floor FloorCinvert(string f)
        {
            switch (f.ToLower())
            {
                case "м":
                    return Floor.М;
                case "ж":
                    return Floor.Ж;
                default:
                    return Floor.М;
            }
        }

        public void SynchronizerTreadAbort()
        {
            SynchronizationThread.Abort();
        }
        ~Synchronizer()
        {
            SynchronizationThread.Abort();
        }
    }
}


